package com.ihr360.expr.function.excel;


import com.ihr360.expr.ExprException;
import com.ihr360.expr.ExprString;
import com.ihr360.expr.context.IEvaluationContext;
import com.ihr360.expr.core.AbstractFunction;
import com.ihr360.expr.core.Expr;
import com.ihr360.expr.utils.Maths;

/**
 * 需要和excel
 */
public class FIXED extends AbstractFunction {
    @Override
    public Expr evaluate(IEvaluationContext context, Expr[] args) throws ExprException {

        if (args.length < 2) {
            throw new ExprException("Too few arguments entered for function FIXED");
        } else if (args.length > 3) {
            throw new ExprException(
                    "Too many arguments entered for function FIXED");
        }
        double num = asDouble(context, args[0], true);
        int dps = asInteger(context, args[1], true);
        boolean no_commas = false;
        if (args.length == 3) {
            no_commas = asBoolean(context, args[2], true);
        }
        double number = Maths.round(num, dps);
        if (no_commas){
            return new ExprString(String.format("%."+dps+"f",number));
        }

        return new ExprString(String.format("%,."+dps+"f",number));
    }
}
